package com.hzhc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzhc.entity.SysYzsRoleMenu;
import com.hzhc.mapper.SysYzsRoleMenuMapper;
import com.hzhc.service.SysYzsRoleMenuService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author dongcy
 * @date 2024/4/12 11:18
 * @description SysYzsRoleMenuServiceImpl
 */
@Service
@RequiredArgsConstructor
public class SysYzsRoleMenuServiceImpl extends ServiceImpl<SysYzsRoleMenuMapper, SysYzsRoleMenu> implements SysYzsRoleMenuService {

    private  final SysYzsRoleMenuMapper sysYzsRoleMenuMapper;

    @Override
    public List<Long> getListIdsByRoleId(Long roleId) {

        List<Long> lret = new ArrayList<Long>();
        QueryWrapper<SysYzsRoleMenu> queryWrapper = new QueryWrapper<SysYzsRoleMenu>();
        queryWrapper.eq("yzs_role_id", roleId);
        List<SysYzsRoleMenu> list = sysYzsRoleMenuMapper.selectList(queryWrapper);
        for (SysYzsRoleMenu rm : list) {
            lret.add(rm.getYzsMenuId());
        }
        return lret;

    }

    @Override
    public Boolean saveOrUpdateRoleMenu(ArrayList<Integer> data, Long roleid) {
// 先删除后添加
        SysYzsRoleMenu yzsRoleMenu = new SysYzsRoleMenu();
        yzsRoleMenu.setYzsRoleId(roleid);
        sysYzsRoleMenuMapper.delete(new QueryWrapper<SysYzsRoleMenu>(yzsRoleMenu));
        for (int i = 0; i < data.size(); i++) {
            Integer mid = data.get(i);
            SysYzsRoleMenu sysYzsRoleMenu = new SysYzsRoleMenu();
            sysYzsRoleMenu.setYzsRoleId(roleid);
            sysYzsRoleMenu.setYzsMenuId(Long.valueOf(mid));
            sysYzsRoleMenuMapper.insert(sysYzsRoleMenu);
        }
        return true;
    }
}
